//
//  SPDataCenterTableViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/19.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPDataCenterTableViewController.h"
#import "SPCommon.h"
#import "SPDataHeaderView.h"
#import "SPUpcomingSectionHeaderView.h"
#import "SPDataCPFHeaderFooterView.h"
#import "SPDataCAreaHeaderFooterView.h"
#import "SPLiveHistoryTableViewController.h"
#import "SPDataCModel.h"



@interface SPDataCenterTableViewController ()
@property (strong, nonatomic) NSArray<NSString *> *sections;
@property (assign, nonatomic) GenderType genderType;
@property (strong, nonatomic) NSArray *man;
@property (strong, nonatomic) NSArray *woman;
@property (strong, nonatomic) NSArray *all;

@property (assign, nonatomic) CGFloat pf_h_all;
@property (assign, nonatomic) CGFloat pf_h_man;
@property (assign, nonatomic) CGFloat pf_h_woman;
@property (nonatomic,assign) CGFloat pf_height;
@property (assign, nonatomic) CGFloat area_height;

@property (nonatomic,strong) SPDataHeaderView *headerV;
@property (nonatomic,strong)  SPDataCModel *dataSource;

@end

@implementation SPDataCenterTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"数据中心";
    [self setupBtn];
    [self setupTableView];
    
    _sections = @[@"人流分析",@"区域分析"];
    _genderType = GenderType_ALL;
    _man = @[@"18-25岁",@"25-30岁",@"30-40岁",@"40-50岁",@"50-60岁"];
    _all = @[@"18-25岁",@"25-30岁",@"30-40岁",@"40-50岁",@"50-60岁",@"60-65岁"];
    _woman = @[@"18-25岁",@"25-30岁",@"30-40岁"];
    [self fetchData];
    
}


- (void)setupTableView
{
//    self.tableView.backgroundColor = RZ_COLOR(245, 245, 245);
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    SPDataHeaderView *headerV = [SPDataHeaderView shareHeaderView];
    headerV.height = 412;
    _headerV = headerV;
    self.tableView.tableHeaderView = headerV;
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableFooterView.height  =10;
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPUpcomingSectionHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass([SPUpcomingSectionHeaderView class])];

}


//设置导航栏右边按钮
- (void)setupBtn
{
    UIButton *historyBtn = [[UIButton alloc] init];
    historyBtn.bounds = CGRectMake(0, 0, 60, 21);
    [historyBtn addTarget:self action:@selector(historyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [historyBtn setTitleColor:RZ_COLOR(155, 155, 155) forState:UIControlStateNormal];
    [historyBtn setTitle:@"历史直播" forState:UIControlStateNormal];
    historyBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:historyBtn];
    
}



#pragma mark--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0;
    switch (section) {
        case 0:
//            height = _genderType == GenderType_WOMAN?205:265;
//            height = UITableViewAutomaticDimension;
//            switch (_genderType) {
//                case GenderType_ALL:
//                    _pf_height = _pf_h_all;
//                    break;
//                case GenderType_MAN:
//                    _pf_height = _pf_h_man;
//                    break;
//                case GenderType_WOMAN:
//                    _pf_height = _pf_h_woman;
//                    break;
//            }
            height = _pf_height == 0?0.01: _pf_height;
            break;
            
        case 1:
//            height = 125;
            height = _area_height == 0?0.01: _area_height;
            break;
    }
    return height;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0.01;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer = nil;
    __weak typeof(self) weakSelf = self;
    switch (section) {
        case 0:{
            SPDataCPFHeaderFooterView *pfV = [SPDataCPFHeaderFooterView shareHeaderView];
            pfV.refreshBlock = ^(GenderType type,CGFloat height) {//切换性别刷新数据
                weakSelf.pf_height = height;
                weakSelf.genderType = type;
                [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
            };
            
            pfV.all = self.all;
            pfV.man = self.man;
            pfV.woman = self.woman;
            pfV.model = self.dataSource;
            pfV.genderType = self.genderType;
            footer = pfV;
            break;
        }
            
        case 1:{
            SPDataCAreaHeaderFooterView *areaV = [SPDataCAreaHeaderFooterView shareHeaderView];
            areaV.refreshBlock = ^(CGFloat height) {
                if(weakSelf.area_height == height) return;
                weakSelf.area_height = height;
                [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
            };
            areaV.citys = self.dataSource.total_man_city;
            footer = areaV;
            break;
        }
    }
    
    return footer;
}



- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPUpcomingSectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SPUpcomingSectionHeaderView class])];
    headerV.title.text = _sections[section];
    headerV.moreBtn.hidden = YES;
    return headerV;
}




- (void)historyBtnClick:(UIButton *)btn
{
    [self.navigationController pushViewController:[SPLiveHistoryTableViewController new] animated:YES];
}



#pragma mark--
- (void)fetchData
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=data_total&token=568e86c8630f7b44f29a23ba211d9f25
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"data_total";
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            self.dataSource = [SPDataCModel mj_objectWithKeyValues:res[@"data"]];
            [self handleData];
        }else{
            
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}



- (void)handleData
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        //计算高度
//        //地区
//        NSInteger count_area = self.dataSource.total_man_city.count;
//        NSInteger row_area = count_area / 4;
//        if(count_area == 0){
//            self.area_height = 0;
//        }else if (count_area % 4 == 0){
//            self.area_height =25+(30+15)*row_area;
//        }else if (count_area % 4 != 0){
//            self.area_height =25+(30+15)*(row_area+1);
//        }
//
//        //人流
//        NSInteger count_pf_all = self.all.count;
//        NSInteger row_pf_all = count_pf_all / 4;
//        if(count_pf_all == 0){
//            self.pf_h_all = 140;
//        }else if (count_pf_all % 4 == 0){
//            self.pf_h_all =140 + 25 + (30+15)*row_pf_all;
//        }else if (count_pf_all % 4 != 0){
//            self.pf_h_all = 140 + 25 + (30+15)*(row_pf_all+1);
//        }
//
//        NSInteger count_pf_man = self.man.count;
//        NSInteger row_pf_man = count_pf_man / 4;
//        if(count_pf_man == 0){
//            self.pf_h_man = 140;
//        }else if (count_pf_man % 4 == 0){
//            self.pf_h_man =140 + 25 + (30+15)*row_pf_man;
//        }else if (count_pf_man % 4 != 0){
//            self.pf_h_man =140 + 25 + (30+15)*(row_pf_man+1);
//        }
//
//        NSInteger count_pf_woman = self.woman.count;
//        NSInteger row_pf_woman = count_pf_woman / 4;
//        if(count_pf_woman == 0){
//            self.pf_h_woman = 140;
//        }else if (count_pf_woman % 4 == 0){
//            self.pf_h_woman = 140 + 25 + (30+15)*row_pf_woman;
//        }else if (count_pf_woman % 4 != 0){
//            self.pf_h_woman = 140 + 25 + (30+15)*(row_pf_woman+1);
//        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
                self.headerV.model = self.dataSource;
                [self.tableView reloadData];
        });
        
    });
}
@end
